//
//  ViewController.swift
//  lookLikeIweekly
//
//  Created by Mainzxq on 16/6/3.
//  Copyright © 2016年 Mainzxq. All rights reserved.
//

import UIKit
import Photos

class ViewController: UIViewController, MyScrollViewwDelegate, PHPhotoLibraryChangeObserver {
     let transition = MagicMoveAnimate()
    var myScroll = TopScrollView()
    var newsArray = [PictureArticleSection]()
    var NewController = NewsViewController()
    @IBOutlet weak var selectImageBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        newsArray = defineNews()
        myScroll = TopScrollView(frame: CGRect(x: 0, y: 100, width: view.frame.size.width, height: CGFloat(3)*view.frame.size.height/CGFloat(4)))
        myScroll.newsList = newsArray
        myScroll.delegate = self
        myScroll.tag = 1031
        myScroll.setUpSubViews()
        view.addSubview(myScroll)
        let toLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipeLeft))
        toLeft.direction = UISwipeGestureRecognizerDirection.Left
        myScroll.contextView.addGestureRecognizer(toLeft)
        view.bringSubviewToFront(selectImageBtn)
        PHPhotoLibrary.sharedPhotoLibrary().registerChangeObserver(self)
        
    }

    
    func tappedImage(currentImage: Int) {
        print(currentImage)
        myScroll.stopAnimate()

        let magnifyView = UIView(frame: self.myScroll.scrollView.frame)
        magnifyView.backgroundColor = UIColor.blackColor()
        let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tap))
        magnifyView.addGestureRecognizer(tap)
        // 需要创建自定义View，直接复用比较好。代码整洁
        let contentLbl = UILabel()
        contentLbl.frame.origin = CGPoint(x: 10, y: magnifyView.center.y+self.myScroll.scrollView.frame.size.height/2+5)
        contentLbl.frame.size = CGSize(width: self.view.frame.size.width-10, height: 40)
        contentLbl.numberOfLines = 0
        contentLbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
        contentLbl.alpha = 0
        contentLbl.font = UIFont(name: "HelveticaNeue-Light", size: CGFloat(14))
        contentLbl.adjustsFontSizeToFitWidth = true
        contentLbl.textColor = UIColor.whiteColor()
        contentLbl.text = newsArray[currentImage-1].article
        contentLbl.tag = 1011
        magnifyView.addSubview(contentLbl)
        let magImageView = UIImageView()
        magImageView.frame.origin = self.myScroll.frame.origin
        magImageView.frame.size = myScroll.scrollView.frame.size
        magImageView.contentMode = .ScaleAspectFill
        magImageView.clipsToBounds = true
        magImageView.image = newsArray[currentImage-1].albumImage ?? newsArray[currentImage-1].getImage()
        magnifyView.addSubview(magImageView)
        view.addSubview(magnifyView)

        UIView.animateWithDuration(0.5) {
            magnifyView.frame = self.view.bounds
            contentLbl.alpha = 1
            contentLbl.frame.origin = CGPoint(x: 10, y: magnifyView.center.y+self.myScroll.scrollView.frame.size.height/2+5)
            magImageView.center = magnifyView.center
        }
        
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    func swipeLeft() {
        
        let viewController = storyboard!.instantiateViewControllerWithIdentifier("secondViewController") as! NewsViewController
         viewController.transitioningDelegate = self
        // viewController.modalTransitionStyle = .CrossDissolve
        presentViewController(viewController, animated: true, completion: nil)
        
        
    }
    
    func tap() {
        UIView.animateWithDuration(0.5, animations: {
            self.view.subviews.last?.frame.origin = CGPoint(x: 0, y: -100)
            self.view.subviews.last?.frame.size.height = CGFloat(3)*self.view.frame.size.height/CGFloat(4)/2+100
            self.view.subviews.last?.viewWithTag(1011)?.frame.origin = CGPoint(x: 10, y: self.view.center.y)
            self.view.subviews.last?.alpha = 0
        }) { (finish:Bool) in
            if finish {
                self.view.subviews.last?.removeFromSuperview()
                self.myScroll.startAnimate()
            }
        }
    }
    
    func photoLibraryDidChange(changeInstance: PHChange) {
        
    }

    @IBAction func performSelecter(sender: AnyObject) {
        let flowLayout = UICollectionViewFlowLayout()
        let photosVC = PHCollectionController(collectionViewLayout: flowLayout)
        
        photosVC.myCallBack{(imageArray, dataArray) in
            self.newsArray = defineNews1(imageArray, assetArray: dataArray)
            self.view.viewWithTag(1031)!.removeFromSuperview()
            let myScroll1 = TopScrollView(frame: CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: CGFloat(3)*self.view.frame.size.height/CGFloat(4)))
            myScroll1.newsList = self.newsArray
            myScroll1.delegate = self
            myScroll1.tag = 1031
            myScroll1.setUpSubViews()
            self.view.addSubview(myScroll1)

       }
        presentViewController(photosVC, animated: true, completion: nil)
    }
   


}


extension ViewController: UIViewControllerTransitioningDelegate {
    
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        return transition
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        return transition
    }
}
